package com.weighttracker.android;

import com.bappi.custom.activities.CustomActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class Tab3Activity22 extends CustomActivity{
	
	private EditText caloriesField,fatField,fiberField,resultFiled;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_tab_3_screen_22);
			caloriesField = (EditText)findViewById(R.id.calories_field);
			fatField = (EditText)findViewById(R.id.fat_field);
			fiberField = (EditText)findViewById(R.id.fiber_field);
			resultFiled = (EditText)findViewById(R.id.result_field);
			findViewById(R.id.button_back).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					finish();
				}
			});
			findViewById(R.id.button_clear).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					fatField.setText("");
					caloriesField.setText("");
					fiberField.setText("");
					resultFiled.setText("");
				}
			});
			findViewById(R.id.button_calculate).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					double fat = 0.0F;
					double calories = 0.0F;
					double fiber = 0.0F;
					String text;
					try {
						text = fatField.getText().toString();
						if(text.length()>0){
							fat = Float.parseFloat(text);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					try {
						text = caloriesField.getText().toString();
						if(text.length()>0){
							calories = Float.parseFloat(text);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					try {
						text = fiberField.getText().toString();
						if(text.length()>0){
							fiber = Float.parseFloat(text);
						}
						if(fiber>4){
							fiber = 4;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					double value =  (calories / 50.0) + (fat / 12.0) - (fiber / 5.0);
//					Log.i("DREG", "calories="+calories);
//					Log.i("DREG", "fat="+fat);
//					Log.i("DREG", "fiber="+fiber);
//					Log.i("DREG", "VALUE="+value);
//					resultFiled.setText(Long.toString((long)Math.floor(value)));				
					resultFiled.setText(Long.toString(Math.round(value)));
				}
			});
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}
